﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;

namespace WaiGaoQiaoWeb.Controllers
{
    public class InventoryInfoSelController : Controller
    {
        private InventoryInfoSelInfo invinfo = new InventoryInfoSelInfo(true);

        public ActionResult InventoryInfoSelIndex()
        {
            ViewData["GetWarehouse"] = GetWarehouse(-1, "请选择...");
            return View();
        }

        /// <summary>
        /// 获取仓库列表
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetWarehouse(int value, string text)
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            WarehouseInfo wareinfo = new WarehouseInfo(true);
            IList<WarehouseInfo> warelist = wareinfo.SelWarehouseInfo(100, 1, "");
            if (text != "")
            {
                dic.Add(value, text);
            }
            foreach (WarehouseInfo wi in warelist)
            {
                dic.Add(wi.ID, wi.Warehouse_Name);
            }
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取主副调食材库存列表
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetInventoryInfoSelList(PageVM pvm)
        {
            string query = " 1=1 ";
            string[] param = pvm.query.Split(',');
            string Warehouse_ID = param[1];
            if (string.IsNullOrEmpty(pvm.query))
            {
                query = " and 1=1";
            }
            else
            {
                query = " and A.GoodsName like'%" + param[0] + "%'";
            }
            int count = 0;
            IList<InventoryInfoSelInfo> invlist = invinfo.SelInventoryInfoSelInfo(1000, pvm.page, query, Warehouse_ID, out count);
            JsonFlexiGridData jfgd = new JsonFlexiGridData();
            jfgd.page = pvm.page;
            jfgd.total = count;
            List<string> v;
            foreach (InventoryInfoSelInfo item in invlist)
            {
                #region
                v = new List<string>();
                v.Add(item.GoodsNo.ToString());
                v.Add(item.GoodsName);
                v.Add(item.PRICE.ToString());
                v.Add(item.STOCKS_COUNT.ToString());
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item.GoodsNo.ToString(),
                    cell = v
                };
                jfgd.rows.Add(fgr);
            }
            return Json(jfgd);
        }
    }
}
